import { http } from '@/utils/http'
import type { UploadProgressCallback } from '@/types/api'

// 用户相关 API
export const userApi = {
  
  //验证码
  getCaptcha:() => {
    return http.get<any>('/index/user/getCaptcha')
  },

  // 登录
  login: (
    data: { 
      username: string; 
      password: string; 
      rememberMe?: boolean;
      code?:string;
      code_key?:string 
    }) => {
    return http.post<{ id: number }>('index/user/login', data)
  },

  // 更新主题
  updateTheme: (data: any) => {
    return http.put<any>('/index/user/set_theme', data)
  },

  // 注册
  register: (data: { username: string; password: string; mail: string }) => {
    return http.post<{ id: number }>('/auth/register', data)
  },

  // 获取用户信息
  getUserInfo: () => {
    return http.get<any>('/api/system_user/info')
  },

  // 获取用户列表
  editUserInfo: (data: any) => {
    return http.post('/api/system_user/editInfo', data)
  },

  // 退出登录
  logout: () => {
    return http.post('/auth/logout')
  }
}


export const systemUserApi = {
  // 获取管理员列表
  getList: (params: any) => http.get('/api/system_user/getList', { params }),
  
  // 新增管理员
  add: (data: any) => http.post('/api/system_user/add', data),
  
  // 编辑管理员
  update: (data: any) => http.post('/api/system_user/update', data),
  
  // 删除管理员
  delete: (id?: number) => http.post('/api/system_user/delete',{id:id}),
  
  // 检查用户名是否存在
  checkUsername: (params: any) => http.get('/api/system_user/checkUsername', { params }),
  
  // 检查手机号是否存在
  checkPhone: (params: any) => http.get('/api/system_user/checkPhone', { params }),

  // 重置密码
  resetPassword: (data: any) => http.post('/api/system_user/resetPassword', data),
  resetPassword2: (data: any) => http.post('/api/system_user/resetPassword2', data),

}


// 文件上传下载 API
export const fileApi = {
  // 上传文件
  upload: (file: File, onProgress?: UploadProgressCallback) => {
    const formData = new FormData()
    formData.append('file', file)
    return http.upload<{ url: string; filename: string; size: number }>(
      '/api/file/upload', 
      formData, 
      onProgress
    )
  },

  // 批量上传
  uploadMultiple: (files: File[], onProgress?: UploadProgressCallback) => {
    const formData = new FormData()
    files.forEach(file => {
      formData.append('files', file)
    })
    return http.upload<Array<{ url: string; filename: string; size: number }>>(
      '/file/upload-multiple',
      formData,
      onProgress
    )
  },

  // 下载文件
  download: (fileId: string, filename?: string) => {
    return http.download('/file/download', { fileId }, filename)
  }
}

// 通用 API
export const commonApi = {
  // 获取配置
  getConfig: () => {
    return http.get<Record<string, any>>('/common/config')
  },

  // 发送反馈
  sendFeedback: (data: { type: string; content: string; contact?: string }) => {
    return http.post('/common/feedback', data)
  }
}

// 导出 http 实例以便特殊需求使用
export { http }